﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
namespace TE3
{
	public class TextureHandler
	{
		public Dictionary<string, Texture2D> Textures;
		private readonly Game _currentGame;
		public TextureHandler(Game currentGame)
		{
			Textures = new Dictionary<string, Texture2D>();
			_currentGame = currentGame;
		}
		public void LoadTextures()
		{
			if (!File.Exists(_currentGame.Content.RootDirectory + "/Cfg/Textures.cfg")) return;
			using (var reader = new StreamReader(_currentGame.Content.RootDirectory + "/Cfg/Textures.cfg"))
			{
				while(!reader.EndOfStream)
				{
					var currentData = reader.ReadLine();
					Textures.Add(currentData, LoadTexture2DFromFile(_currentGame.Content.RootDirectory + "/" + currentData,_currentGame.GraphicsDevice));
					Console.WriteLine("Texture added: " + currentData);
				}
			}
		}
		public static Texture2D LoadTexture2DFromFile(string filePath, GraphicsDevice device)
		{
			var fileStream = new FileStream(filePath, FileMode.Open);
			var tempTexture = Texture2D.FromStream(device, fileStream);
			fileStream.Close();
			return tempTexture;
		}
	}
}
